package com.aliyun.messagemgt.application.service.Impl;


import com.aliyun.messagemgt.application.service.MsgHandleLogService;
import com.aliyun.messagemgt.application.service.MsgResend;
import com.aliyun.messagemgt.application.service.PushEngine;
import com.aliyun.messagemgt.common.constant.PushConstant;
import com.aliyun.messagemgt.common.dto.send.MsgMiddleResult;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.common.dto.send.Receiver;
import com.aliyun.messagemgt.common.enums.PushResultEnum;
import com.aliyun.messagemgt.dto.MsgSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/4/4
 * @time:10:41
 */
@Component
public class MsgResendImpl implements MsgResend {

    private static final Logger logger = LoggerFactory.getLogger(MsgResendImpl.class);

    private static final Set<Integer> RESEND_ERRORCODE = new HashSet<>();

    @Autowired
    private PushEngine pushEngine;
    @Autowired
    private MsgHandleLogService msgHandleLogService;

    static {
        // 确认重发及扩展
        // 预先写好可以重发对应的错误码
        RESEND_ERRORCODE.add(PushResultEnum.PUSH_TOKEN_INVALID.getErrorCode());
        RESEND_ERRORCODE.add(PushResultEnum.WEIXIN_PUSH_ERROR.getErrorCode());
    }

    /**
     * 1.判断改消息源有没有配置重发规则，若无则不需处理
     * 2.取出需要重发的数据
     * 3.重新发送
     *
     * @param msgRecordVos
     * @param msgSource
     */
    @Override
    public void resendMsg(List<MsgRecordVO> msgRecordVos, MsgSource msgSource) {
        Integer resendChannelId = msgSource.getResendChannelId();
        if (resendChannelId == null) {
            logger.info("{} 未配置重发规则", msgSource);
            return;
        }
        // 判断哪些数据需要重发，加入到重发List中
        for (MsgRecordVO msgRecordVO : msgRecordVos) {
            List<MsgMiddleResult> results = msgRecordVO.getResults();
            List<Receiver> resendReceiver = new ArrayList<>();
            for (MsgMiddleResult result : results) {
                if (RESEND_ERRORCODE.contains(result.getPushResultCode())) {
                    resendReceiver.addAll(result.getReceivers());
                }
            }
            if (resendReceiver.size() > 0) {
                MsgRecordVO msg = new MsgRecordVO().setReceivers(resendReceiver)
                        .setMsgTitle(msgRecordVO.getMsgTitle())
                        .setMsgDescription(msgRecordVO.getMsgDescription())
                        .setMessageType(msgRecordVO.getMessageType())
                        .setContents(msgRecordVO.getContents())
                        .setPushChannelId(msgSource.getResendChannelId().toString())
                        .setPushChannelName(msgSource.getResendChannelName())
                        .setMsgSourceId(msgRecordVO.getMsgSourceId())
                        .setMsgSourceName(msgRecordVO.getMsgSourceName())
                        .setMsgSourceType(PushConstant.RESEND_MSG_SOURCE_TYPE)
                        .setCommentType(msgRecordVO.getCommentType())
                        .setOwnerDept(msgRecordVO.getOwnerDept())
                        .setOwnerDeptName(msgRecordVO.getOwnerDeptName())
                        .setTemplateName(msgRecordVO.getTemplateName());
                List<MsgRecordVO> msgList = Arrays.asList(msg);
                // 推送
                pushEngine.push(msgList);
                // 回写重发结果
                msgRecordVO.setResendResults(msgList.get(0).getResults());
            }
        }
    }
}
